<template>
  <div>
    <el-dialog
      :title="info.title"
      :visible.sync="info.isshow"
      @click="cancel"
      @close="close"
    >
      <el-form :model="formData" ref="formData" :rules="rules">
        <el-form-item label="类型" prop="type" label-width="100px">
          <el-radio-group v-model="formData.type">
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-show="formData.type === 2"
          label="上级"
          prop="pid"
          label-width="100px"
        >
          <el-select v-model="formData.pid">
            <el-option label="顶级目录" :value="0"></el-option>
            <el-option
              v-for="(item, index) in Menulist"
              :key="index"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title" label-width="100px">
          <el-input v-model="formData.title"></el-input>
        </el-form-item>
        <el-form-item
          v-show="formData.type === 1"
          label="图标"
          prop="icon"
          label-width="100px"
        >
          <el-input v-model="formData.icon"></el-input>
        </el-form-item>
        <el-form-item
          v-show="formData.type === 2"
          label="地址"
          prop="url"
          label-width="100px"
        >
          <el-input v-model="formData.url"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" label-width="100px">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ["info"],
  data() {
    return {
      formData: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      validate: {},
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
      Menulist: [],
    };
  },
  created() {
    this.getSelectData();
  },
  methods: {
    setValue(data) {
      console.log(data);
      this.formData = { ...data };
    },
    getSelectData() {
      this.$http
        .get("menulist?istree=1")
        .then((res) => {
          console.log(res);
          this.Menulist = res.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    submitForm() {
      this.isshow = false;
      this.$refs.formData.validate((valid) => {
        let url = "";
        if (valid) {
          if (this.formData.id) {
            //有id是修改数据
            url = "/menuedit";
          } else {
            url = "/menuadd";
          }
          this.$http
            .post(url, this.formData)
            .then((res) => {
              console.log(res);
              if (res.list.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: "success",
                });
                
              }
              this.$emit("close");
              this.$emit("updata");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("err");
          return false;
        }
      });
    },
    cancel() {
      //   this.isshow = false;
      this.$emit("close");
    },
    close() {
      //   this.isshow = false;
      this.$emit("close");
    },
    edit() {
      this.info.isshow = true;
      this.info.title = "修改菜单";
      let data = data;
    },
  },
};
</script>
<style>
</style>